#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "MyString.h"
#include "DynamicArray.h"
#include "Tool.h"



struct Treasure *CreateTreasure(const char *name, int blood, int ap)
{
    struct Treasure *treasure = (struct Treasure *)malloc(sizeof(struct Treasure));
    if (treasure == NULL)
    {
        printf("Create Treasure Error!\n");
        return NULL;
    }
    treasure->blood = blood;
    treasure->ap = ap;
    Initiallize(&treasure->name, name);

    treasure->x = GetRandNumber(Map_Size);
    treasure->y = GetRandNumber(Map_Size);
    while (treasure->x == 0 && treasure->y == 0)
    {
        treasure->x = GetRandNumber(Map_Size);
        treasure->y = GetRandNumber(Map_Size);
    }

    return treasure;
}

void InitTreasure(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }

    InsertArray(array, CreateTreasure("宝藏", 30, 12));
    InsertArray(array, CreateTreasure("珍珠", 20, 6));
    InsertArray(array, CreateTreasure("珍珠", 20, 6));
    InsertArray(array, CreateTreasure("珍珠", 20, 6));
    InsertArray(array, CreateTreasure("药水", 10, 3));
    InsertArray(array, CreateTreasure("药水", 10, 3));
    InsertArray(array, CreateTreasure("药水", 10, 3));
    InsertArray(array, CreateTreasure("药水", 10, 3));
    InsertArray(array, CreateTreasure("药水", 10, 3));
}

int Gain(struct Player *player, struct Treasure *treasure)
{
    system("clear");
    printf("                    您遇了|%s|  增加攻击力:|%d|  增加血条:|%d|\n", treasure->name.string, treasure->ap, treasure->blood);
    printf("*********************************************************************************************\n");
    printf("*                                           发现宝藏                                         *\n");
    printf("*********************************************************************************************\n");
    while (player->HP > 0)
    {
        printf("                获得了一个宝贝\n");

        player->HP += treasure->blood;
        player->attack += treasure->ap;

        printf("                |%s|对你增加|%d|攻击力\n", treasure->name.string, treasure->ap);
        printf("                |%s|对你增加|%d|生命力\n", treasure->name.string, treasure->blood);
        treasure->blood -= treasure->blood;
        treasure->ap -= treasure->ap;
        printf("                你当前血量|%d|攻击力|%d| \n", player->HP, player->attack);
        printf("                你当前所在的位置是<%d , %d>\n ", player->x, player->y);
        break;
    }
    printf("========================================end=============================================\n");
    sleep(1);
}